{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Executor "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ebb0705d6a05459a89f4ae87cbbbfd84",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Evaluating:   0%|          | 0/10 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ragas.executor import Executor\n",
    "from asyncio import sleep\n",
    "\n",
    "exec = Executor(raise_exceptions=True)\n",
    "for i in range(10):\n",
    "    exec.submit(sleep, i / 10)\n",
    "\n",
    "assert exec.results(), \"didn't get anything from results\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ragas.executor import is_event_loop_running\n",
    "\n",
    "assert is_event_loop_running() is True, \"is_event_loop_running() returned False\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ragas.executor import as_completed\n",
    "import asyncio\n",
    "\n",
    "\n",
    "async def echo_order(index: int):\n",
    "    await asyncio.sleep(index)\n",
    "    return index\n",
    "\n",
    "\n",
    "async def _run():\n",
    "    results = []\n",
    "    for t in as_completed([echo_order(1), echo_order(2), echo_order(3)], 3):\n",
    "        r = await t\n",
    "        results.append(r)\n",
    "    return results\n",
    "\n",
    "\n",
    "results = await _run()\n",
    "\n",
    "expected = [1, 2, 3]\n",
    "assert results == expected, f\"got: {results}, expected: {expected}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "985b8a189c9047c29d6ccebf7c5a938b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Evaluating:   0%|          | 0/1000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ff3097e24dc249fbab6e610e59ccc9b6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Evaluating:   0%|          | 0/2000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "exec = Executor(raise_exceptions=True)\n",
    "for i in range(1000):\n",
    "    exec.submit(sleep, 0.01)\n",
    "\n",
    "assert exec.results(), \"didn't get anything from results\"\n",
    "\n",
    "for i in range(1000):\n",
    "    exec.submit(sleep, 0.01)\n",
    "\n",
    "assert exec.results(), \"didn't get anything from results\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Metric"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ragas.metrics.base import Metric, EvaluationMode\n",
    "\n",
    "\n",
    "class FakeMetric(Metric):\n",
    "    name = \"fake_metric\"\n",
    "    evaluation_mode = EvaluationMode.qa\n",
    "\n",
    "    def init(self):\n",
    "        pass\n",
    "\n",
    "    async def _ascore(self, row, callbacks) -> float:\n",
    "        return 0\n",
    "\n",
    "\n",
    "fm = FakeMetric()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = fm.score(\n",
    "    row={\"question\": [\"q\"], \"answer\": [\"a\"]},\n",
    "    callbacks=None,\n",
    ")\n",
    "assert score == 0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ragas",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
